import { dateFormat } from '@/common/utils/common'
import { enumOrgClass } from '@/credit/plugins/enum'
// import { getMonthLastDayDate } from '@/common/plugins/tools/date'

export const enumReportRule = {
  1: '月',
  2: '季',
  3: '年',
}

export const enumReportRuleName = {
  1: '按月份生成',
  2: '按季度生成',
  3: '按年度生成',
}

export const enumReportType = {
  1: '新增项目',
  2: '在保项目',
  3: '解保项目',
  4: '风险项目',
}

export const enumPovertyProject = {
  0: '否',
  1: '是',
}

export const enumBuyInsurance = {
  0: '否',
  1: '是',
}

export const enumRiskType = {
  1: '逾期',
  2: '代偿',
}

export const enumCountryRiskType = {
  0: '不正常',
  1: '正常',
}

export const reportDataAdapter = (item, index) => {
  item.index = index + 1
  item.customerTypeName = enumOrgClass[item.org_class] || '-'
  item.isPovertyProject = enumPovertyProject[item.is_poverty_project] || '-'
  item.isDualControl = enumBuyInsurance[item.is_dual_control] || '-'
  item.load_beh_status = +(item.load_beh_status || 0)
  item.loanBehStatus = item.load_beh_status === 6 ? '解保' : '在保'
  item.unGuaTime = dateFormat(item.un_gua_time, 'yyyy-MM-dd')
  item.isBuyInsurance = enumBuyInsurance[item.is_buy_insurance] || '-'

  item.risk_type = +item.risk_type || null
  item.risk_type_name = enumRiskType[item.risk_type] || '-'
  item.riskTye = enumCountryRiskType[item.risk_tye] || '-'
  item.loadDate = dateFormat(item.load_date, 'yyyy-MM-dd')
  item.loadEndDate = dateFormat(item.load_end_date, 'yyyy-MM-dd')
  item.isBatch = +item.gua_type === 3 ? '是' : '否'

  return item
}

const numberParser = v => {
  const res = Number.parseFloat(v)
  return Number.isNaN(res) ? v || '' : res
}

export const reportDataExportAdapter = (item, index) => {
  item = reportDataAdapter(item, index)
  item.warranty_amount = {
    v: numberParser(item.warranty_amount),
    t: 'n',
    z: '0.00',
  }
  item.warranty_rate = {
    v: numberParser(item.warranty_rate),
    t: 'n',
    z: '0.00',
  }
  item.warranty_fee = {
    v: numberParser(item.warranty_fee),
    t: 'n',
  }
  item.loadDate = {
    v: item.loadDate,
    t: 'd',
    z: 'yyyy-mm-dd',
  }
  item.loadEndDate = {
    v: item.loadEndDate,
    t: 'd',
    z: 'yyyy-mm-dd',
  }
  return item
}

export const getParams = ({
  clientId,
  startDate,
  endDate,
  addressCode,
  bankId,
  bankCode,
  isProGua, // 是否省直项目
  guaType, // 业务类型：市场化、限率
  busStatus, // 贷后状态：0在保 1解保 2新增
  mainBus, // 十二大
  fundsType, // 金额分类 -1：全部，0：10万以下...
  ...otherParams
}) => {
  const params = otherParams
  params['service_id.eq'] = clientId

  params.address_code = addressCode
  params['bank_code.endlike'] = bankCode || ''
  params['bank_id.eq'] = bankId || ''
  params['gua_type.in'] = guaType === void 0 ? '' : guaType || '1,2,4'
  params['is_pro_gua.eq'] = isProGua || ''
  const hasDate = startDate && endDate
  if (hasDate) {
    const start = dateFormat(startDate, 'yyyy-MM-dd')
    const end = `${dateFormat(endDate, 'yyyy-MM-dd')} 23:59:59`
    switch (busStatus) {
      case 0: // 在保
        params['load_date.lte'] = end
        params['orWhere.un_gua_time.is'] = 'null'
        params['orWhere.un_gua_time.gt'] = end
        break
      case 1: // 解保
        params['un_gua_time.gte'] = start
        params['un_gua_time.lte'] = end
        break
      case 2: // 新增
        params['load_date.gte'] = start
        params['load_date.lte'] = end
        break
      case 3: // 代偿
        params['load_date.gte'] = start
        params['load_date.lte'] = end
        params['is_compens.eq'] = 1
        break
      default:
        break
    }
  }

  // 十二大
  params['main_business.eq'] = mainBus || ''

  // 金额分类
  if (fundsType === -1 || fundsType === void 0) {
    params['warranty_amount.lt'] = ''
    params['warranty_amount.gt'] = ''
    params['warranty_amount.gte'] = ''
    params['warranty_amount.lte'] = ''
  } else if (fundsType === 0) {
    params['warranty_amount.lt'] = '10'
  } else if (fundsType === 1) {
    params['warranty_amount.gte'] = '10'
    params['warranty_amount.lte'] = '300'
  } else if (fundsType === 2) {
    params['warranty_amount.gt'] = '300'
    params['warranty_amount.lte'] = '1000'
  } else {
    params['warranty_amount.gt'] = '1000'
  }

  return params
}

// 报表
export const reportTableColumns = [
  { attrs: { label: '序号', prop: 'index', align: 'center', minWidth: 50 } },
  { attrs: { label: '贷款银行', prop: 'bank_name', align: 'center', minWidth: 160 } },
  { attrs: { label: '企业名称', prop: 'customer_name', align: 'center', minWidth: 160 } },
  { attrs: { label: '客户类型', prop: 'org_class_name', align: 'center', minWidth: 160 } },
  { attrs: { label: '主营业务', prop: 'parent_main_business', align: 'center', minWidth: 170 } },
  {
    attrs: { label: '销售收入(万元)', prop: 'sale_income', align: 'center', minWidth: 120 },
    process: v => v || '-',
  },
  {
    attrs: { label: '资产合计(万元)', prop: 'assets_funds', align: 'center', minWidth: 120 },
    process: v => v || '-',
  },
  { attrs: { label: '实际控制人', prop: 'corp_name', align: 'center', minWidth: 100 } },
  { attrs: { label: '联系电话', prop: 'corp_phone', align: 'center', minWidth: 120 } },
  { attrs: { label: '担保金额(万元)', prop: 'warranty_amount', align: 'center', minWidth: 120 } },
  { attrs: { label: '贷款利率(%)', prop: 'load_rate', align: 'center', minWidth: 100 } },
  {
    attrs: { label: '保证合同号', prop: 'gua_contract', align: 'center', minWidth: 160 },
    process: v => v || '-',
  },
  { attrs: { label: '担保责任发生日期', prop: 'loadDate', align: 'center', minWidth: 140 } },
  { attrs: { label: '担保责任解除日期', prop: 'loadEndDate', align: 'center', minWidth: 140 } },
  { attrs: { label: '贷款期限', prop: 'warranty_period', align: 'center', minWidth: 80 } },
  {
    attrs: { label: '反担保措施(详细)', prop: 'warranty_method', align: 'center', minWidth: 160 },
    slot: 'warranty_method',
  },
  {
    attrs: { label: '风险分担比例', prop: 'risk_rate', align: 'center', minWidth: 100 },
    process: v => v || '-',
  },
  { attrs: { label: '是否为扶贫项目', prop: 'isPovertyProject', align: 'center', minWidth: 120 } },
  { attrs: { label: '综合担保费率', prop: 'warranty_rate', align: 'center', minWidth: 100 } },
  {
    attrs: { label: '综合担保费金额(元)', prop: 'warranty_fee', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  {
    attrs: {
      label: '贷款其他成本',
      prop: 'other_cost',
      align: 'center',
      minWidth: 100,
      className: 'input-td bg-input',
    },
    slot: 'otherCost',
  },
  {
    attrs: {
      label: '担保公司名称',
      prop: 'recommend_service_name',
      align: 'center',
      minWidth: 160,
    },
  },
  {
    attrs: { label: '提交时间', prop: 'submit_time', align: 'center', minWidth: 120 },
    process: v => v || '-',
  },
  {
    attrs: { label: '备注', prop: 'remark', align: 'center', minWidth: 120 },
    process: v => v || '-',
  },
]

// 风险项目报表
export const reportRiskTableColumns = [
  { attrs: { label: '序号', prop: 'index', align: 'center', minWidth: 50 } },
  {
    attrs: { label: '基本信息', align: 'center' },
    columns: [
      { attrs: { label: '客户名称', prop: 'customer_name', align: 'center', minWidth: 160 } },
      {
        attrs: {
          label: '项目状态',
          prop: 'risk_type_name',
          align: 'center',
          minWidth: 120,
          className: 'input-td bg-input',
        },
        slot: 'risk_type_name',
      },
      { attrs: { label: '行业', prop: 'main_business', align: 'center', minWidth: 160 } },
      {
        attrs: { label: '主营业务', prop: 'parent_main_business', align: 'center', minWidth: 170 },
      },
      { attrs: { label: '贷款机构', prop: 'bank_name', align: 'center', minWidth: 160 } },
      { attrs: { label: '产品名称', prop: 'project_type_name', align: 'center', minWidth: 100 } },
    ],
  },
  {
    attrs: { label: '额度信息', align: 'center' },
    columns: [
      { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center', minWidth: 120 } },
      {
        attrs: {
          label: '逾期金额(万元)',
          prop: 'overdue_funds',
          align: 'center',
          minWidth: 120,
          className: 'input-td bg-input',
        },
        slot: 'overdue_funds',
      },
      {
        attrs: { label: '银行分险', align: 'center' },
        columns: [
          {
            attrs: {
              label: '内容',
              prop: 'bank_risk_content',
              align: 'center',
              minWidth: 100,
              className: 'input-td bg-input',
            },
            slot: 'bank_risk_content',
          },
          {
            attrs: {
              label: '分险比例',
              prop: 'bank_risk_rate',
              align: 'center',
              minWidth: 100,
              className: 'input-td bg-input',
            },
            slot: 'bank_risk_rate',
          },
        ],
      },
      {
        attrs: { label: '政府分险', align: 'center' },
        columns: [
          {
            attrs: {
              label: '内容',
              prop: 'gov_risk_content',
              align: 'center',
              minWidth: 100,
              className: 'input-td bg-input',
            },
            slot: 'gov_risk_content',
          },
          {
            attrs: {
              label: '分险比例',
              prop: 'gov_risk_rate',
              align: 'center',
              minWidth: 100,
              className: 'input-td bg-input',
            },
            slot: 'gov_risk_rate',
          },
        ],
      },
      {
        attrs: { label: '地方担保公司等其他机构', align: 'center' },
        columns: [
          {
            attrs: {
              label: '内容',
              prop: 'db_risk_content',
              align: 'center',
              minWidth: 100,
              className: 'input-td bg-input',
            },
            slot: 'db_risk_content',
          },
          {
            attrs: {
              label: '分险比例',
              prop: 'db_risk_rate',
              align: 'center',
              minWidth: 100,
              className: 'input-td bg-input',
            },
            slot: 'db_risk_rate',
          },
        ],
      },
      {
        attrs: {
          label: '应代偿额(万元)',
          prop: 'due_compen_funds',
          align: 'center',
          minWidth: 100,
          className: 'input-td bg-input',
        },
        slot: 'due_compen_funds',
      },
      {
        attrs: {
          label: '截至本季度末累计已代偿额(万元)',
          prop: 'compened_funds',
          align: 'center',
          minWidth: 120,
          className: 'input-td bg-input',
        },
        slot: 'compened_funds',
      },
    ],
  },
  {
    attrs: { label: '日期信息', align: 'center' },
    columns: [
      { attrs: { label: '发放日', prop: 'loadDate', align: 'center', minWidth: 100 } },
      { attrs: { label: '到期日', prop: 'loadEndDate', align: 'center', minWidth: 100 } },
      {
        attrs: {
          label: '逾期日期',
          prop: 'overdue_date',
          align: 'center',
          minWidth: 120,
          className: 'input-td bg-input',
        },
        slot: 'overdue_date',
      },
      {
        attrs: {
          label: '代偿宽限期(天)',
          prop: 'compen_grace_days',
          align: 'center',
          minWidth: 100,
          className: 'input-td bg-input',
        },
        slot: 'compen_grace_days',
      },
      {
        attrs: {
          label: '代偿日',
          prop: 'compen_date',
          align: 'center',
          minWidth: 100,
          className: 'input-td bg-input',
        },
        slot: 'compen_date',
      },
    ],
  },
  {
    attrs: { label: '代偿回收情况', align: 'center' },
    columns: [
      {
        attrs: {
          label: '出险原因',
          prop: 'risk_reason',
          align: 'center',
          minWidth: 100,
          className: 'input-td bg-input',
        },
        slot: 'risk_reason',
      },
      {
        attrs: { label: '反担保措施', align: 'center' },
        columns: [
          {
            attrs: {
              label: '反担保人',
              prop: 'gua_person',
              align: 'center',
              minWidth: 80,
              className: 'input-td bg-input',
            },
            slot: 'gua_person',
          },
          {
            attrs: {
              label: '反担保物',
              prop: 'gua_thing',
              align: 'center',
              minWidth: 80,
              className: 'input-td bg-input',
            },
            slot: 'gua_thing',
          },
        ],
      },
      {
        attrs: {
          label: '反担保措施备注',
          prop: 'gua_method_remark',
          align: 'center',
          minWidth: 80,
          className: 'input-td bg-input',
        },
        slot: 'gua_method_remark',
      },
      {
        attrs: {
          label: '追偿措施',
          prop: 'process_compen',
          align: 'center',
          minWidth: 100,
          className: 'input-td bg-input',
        },
        slot: 'process_compen',
      },
      {
        attrs: {
          label: '截至本季度末累计追回金额',
          prop: 'recover_funds',
          align: 'center',
          minWidth: 100,
          className: 'input-td bg-input',
        },
        slot: 'recover_funds',
      },
      {
        attrs: { label: '-', align: 'center' },
        columns: [
          {
            attrs: {
              label: '1)向客户或反担保人回收金额(含处置反担保物)',
              prop: 'recover_funds_person',
              align: 'center',
              minWidth: 200,
              className: 'input-td bg-input',
            },
            slot: 'recover_funds_person',
          },
          {
            attrs: {
              label: '2)政府分险金额',
              prop: 'gov_risk_funds',
              align: 'center',
              minWidth: 120,
              className: 'input-td bg-input',
            },
            slot: 'gov_risk_funds',
          },
          {
            attrs: {
              label: '3)地方担保、再担保、保险等其他机构分险金额',
              prop: 'db_risk_funds',
              align: 'center',
              minWidth: 200,
              className: 'input-td bg-input',
            },
            slot: 'db_risk_funds',
          },
          {
            attrs: {
              label: '2)其他情况',
              prop: 'other_funds',
              align: 'center',
              minWidth: 120,
              className: 'input-td bg-input',
            },
            slot: 'other_funds',
          },
        ],
      },
    ],
  },
  { attrs: { label: '上报机构', prop: 'submit_agency_name', align: 'center', minWidth: 120 } },
  { attrs: { label: '上报时间', prop: 'submit_time', align: 'center', minWidth: 120 } },
  { attrs: { label: '备注', prop: 'remark', align: 'center', minWidth: 100 } },
]

// 市场化项目台账
export const ledgerTableColumns = [
  { attrs: { label: '序号', prop: 'index', align: 'center', minWidth: 50 } },
  { attrs: { label: '贷款银行', prop: 'bank_name', align: 'center', minWidth: 160 } },
  {
    attrs: { label: '企业名称/贷款人名称', prop: 'customer_name', align: 'center', minWidth: 200 },
  },
  { attrs: { label: '担保金额(万元)', prop: 'warranty_amount', align: 'center', minWidth: 120 } },
  {
    attrs: { label: '在保余额(万元)', prop: 'left_funds', align: 'center', minWidth: 120 },
    process: v => v ?? '-',
  },
  { attrs: { label: '担保责任发生日期', prop: 'loadDate', align: 'center', minWidth: 140 } },
  { attrs: { label: '担保责任解除日期', prop: 'loadEndDate', align: 'center', minWidth: 140 } },
  {
    attrs: { label: '项目状态', prop: 'loanBehStatus', align: 'center', minWidth: 120 },
    process: (v, row) => `${v}${row.load_beh_status === 6 ? `(${row.unGuaTime || '-'})` : ''}`,
  },
  { attrs: { label: '客户类型', prop: 'org_class_name', align: 'center', minWidth: 160 } },
  {
    attrs: { label: '十二大产业分类', prop: 'main_business', align: 'center', minWidth: 200 },
    process: v => v || '-',
  },
  {
    attrs: { label: '主营业务', prop: 'parent_main_business', align: 'center', minWidth: 170 },
    process: v => v || '-',
  },
  {
    attrs: { label: '导入日期', prop: 'create_time', align: 'center', minWidth: 120 },
    process: v => dateFormat(v, 'yyyy-MM-dd'),
  },
  {
    attrs: { label: '营业执照号', prop: 'register_no', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  { attrs: { label: '负责人名称', prop: 'corp_name', align: 'center', minWidth: 140 } },
  {
    attrs: { label: '身份证号', prop: 'lender_idcard', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  { attrs: { label: '联系电话', prop: 'corp_phone', align: 'center', minWidth: 120 } },
  { attrs: { label: '贷款利率(%)', prop: 'load_rate', align: 'center', minWidth: 100 } },
  {
    attrs: { label: '保证合同号', prop: 'gua_contract', align: 'center', minWidth: 160 },
    process: v => v || '-',
  },
  { attrs: { label: '贷款期限', prop: 'warranty_period', align: 'center', minWidth: 80 } },
  {
    attrs: { label: '反担保措施(详细)', prop: 'warranty_method', align: 'center', minWidth: 160 },
    slot: 'warranty_method',
  },
  {
    attrs: { label: '风险分担比例', prop: 'risk_rate', align: 'center', minWidth: 100 },
    process: v => v || '-',
  },
  { attrs: { label: '是否为扶贫项目', prop: 'isPovertyProject', align: 'center', minWidth: 120 } },
  { attrs: { label: '综合担保费率', prop: 'warranty_rate', align: 'center', minWidth: 100 } },
  {
    attrs: { label: '综合担保费金额(元)', prop: 'warranty_fee', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  {
    attrs: {
      label: '担保机构',
      prop: 'recommend_service_name',
      align: 'center',
      minWidth: 160,
    },
  },
  { attrs: { label: '贷款用途', prop: 'funds_use_dir', align: 'center', minWidth: 140 } },
  {
    attrs: { label: '备注', prop: 'remark', align: 'center', minWidth: 120 },
    process: v => v || '-',
  },
]

// 十二贷项目台账
export const ledgerTwelveTableColumns = [
  { attrs: { label: '序号', prop: 'index', align: 'center', minWidth: 50 } },
  { attrs: { label: '贷款银行分支机构', prop: 'bank_name', align: 'center', minWidth: 160 } },
  { attrs: { label: '贷款人/担保客户', prop: 'customer_name', align: 'center', minWidth: 200 } },
  { attrs: { label: '客户类型', prop: 'org_class_name', align: 'center', minWidth: 160 } },
  {
    attrs: { label: '十二大产业分类', prop: 'main_business', align: 'center', minWidth: 160 },
    process: v => v || '-',
  },
  {
    attrs: { label: '主营业务', prop: 'parent_main_business', align: 'center', minWidth: 170 },
    process: v => v || '-',
  },
  {
    attrs: {
      label: '贷款金额/拟担保金额',
      prop: 'warranty_amount',
      align: 'center',
      minWidth: 140,
    },
  },
  { attrs: { label: '放款日/拟担保起始日', prop: 'loadDate', align: 'center', minWidth: 140 } },
  { attrs: { label: '到期日', prop: 'loadEndDate', align: 'center', minWidth: 100 } },
  { attrs: { label: '贷款期限', prop: 'warranty_period', align: 'center', minWidth: 120 } },
  { attrs: { label: '贷款利率(年)', prop: 'load_rate', align: 'center', minWidth: 120 } },
  {
    attrs: { label: '担保方式', prop: 'warranty_method', align: 'center', minWidth: 160 },
    slot: 'warranty_method',
  },
  { attrs: { label: '是否购买农业保险', prop: 'isBuyInsurance', align: 'center', minWidth: 140 } },
  { attrs: { label: '贷款用途', prop: 'funds_use_dir', align: 'center', minWidth: 140 } },
  { attrs: { label: '是否为扶贫项目', prop: 'isPovertyProject', align: 'center', minWidth: 140 } },
  {
    attrs: { label: '征信逾期情况', align: 'center' },
    columns: [
      {
        attrs: { label: '逾期次数', prop: 'overdue_count', align: 'center', minWidth: 140 },
        process: v => v || '-',
      },
      {
        attrs: { label: '逾期合计金额', prop: 'overdue_funds', align: 'center', minWidth: 140 },
        process: v => v || '-',
      },
    ],
  },
  { attrs: { label: '联系电话', prop: 'corp_phone', align: 'center', minWidth: 140 } },
  {
    attrs: {
      label: '客户身份证号码/统一社会信用代码',
      prop: 'lender_idcard',
      align: 'center',
      minWidth: 140,
    },
    process: v => v || '-',
  },
  { attrs: { label: '客户住址/经营地址', prop: 'address', align: 'center', minWidth: 140 } },
  {
    attrs: { label: '项目状态(在保/解保)', prop: 'loanBehStatus', align: 'center', minWidth: 120 },
  },
]

export const enumQuarter = {
  1: '一',
  2: '二',
  3: '三',
  4: '四',
}

export const isQuarterDisabled = (year, quarter) => {
  const currentYear = new Date().getFullYear()
  const currentMonth = new Date().getMonth()

  return (
    year < 2020 ||
    (year === 2020 && quarter < 2) ||
    (currentYear === year ? quarter * 3 > currentMonth : currentYear < year)
  )
}

export const enumBusType = [
  { value: 0, label: '直担' },
  { value: 1, label: '联担' },
]

export const enumProjectType = [
  { value: 0, label: '在保', count: 0 },
  { value: 1, label: '解保', count: 0 },
  { value: 2, label: '新增', count: 0 },
]
